package com.gurun.camera.main.model;

import com.danale.sdk.Danale;
import com.danale.sdk.platform.entity.device.Device;
import com.danale.sdk.platform.result.device.GetDeviceListResult;
import com.danale.sdk.platform.result.user.LogoutResult;
import com.gurun.camera.main.cache.DeviceCache;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.functions.Func1;

public class MainModelImpl implements MainModel {

    /**
     * 获取设备列表
     * @param
     * @return
     */
    @Override
    public Observable<List<Device>> getDevices() {

            Observable<List<Device>> listObservable = Danale.get()
                    .getPlatformDeviceService()
                    .getDeviceList(1890, 1, Integer.MAX_VALUE)
                    .flatMap(new Func1<GetDeviceListResult, Observable<List<Device>>>() {
                        @Override
                        public Observable<List<Device>> call(GetDeviceListResult getDeviceListResult) {
                            List<String> ids = new ArrayList<String>();
                            List<Device> devices = getDeviceListResult.getDeviceList();
                            if (null != devices && !devices.isEmpty()) {
                                for (Device device : devices) {
                                    ids.add(device.getDeviceId());
                                    DeviceCache.getInstance().updateDevice(device);
                                }
                            }
                            return Observable.just(devices);
                        }
                    });
            return listObservable;

    }

    /**
     * 注销 logout
     * @return
     */

    @Override
    public Observable<LogoutResult> logout() {
        return Danale.get().getAccountService().logout(0);
    }


}
